<?php
namespace Admin\Controller;
use Common\Controller\CoreController;
class ProductController extends CoreController {
	public function Index(){

		
		$ComModel = M('Commodity');
        $ComList = $ComModel->order('id desc')->page($page.',20')->select();
       
        $this->assign('list',$ComList);
        
        $count = $ComModel->count();
        $Page = new \Think\Page($count,20);
        $show = $Page->show();
        $this->assign('page',$show);

        $this->assign('addText','添加商品');
        $this->assign('addNew',U('addProduct'));
        $this->assign('edit',U('editProduct'));
        $this->assign('del',U('delProduct'));
        $this->display();

        return getCategoryForm();
        return getCategoryPid();
	}

	public function addProduct(){
		$this->categoryForm=getCategoryForm($cates);
		if(IS_POST){
			//写入商品表
    		$data = array(
    			'com_name' => I('post.com_name'),
    			'com_number' => I('post.com_number'),
    			'com_keywords' => I('post.com_keywords'),
    			'com_discription' => I('post.com_discription'),
    			'com_store' => I('post.com_store'),
    			'com_price' => I('post.com_price'),
    			'com_details' => I('post.com_details'),
    			'com_addtime' => time(),
    			'status' => I('post.status',0,'intval'),
    			);
    		$com=M('Commodity')->add($data);
    		//写入图片表
    		if ($com) {
    			foreach (I('post.banner') as $key => $value) {
    				$img[$key]['com_img_name']=$value;
    			}
    			foreach (I('post.keywords') as $key => $value) {
    				$img[$key]['com_img_keyword']=$value;
    			}
    			foreach (I('post.link') as $key => $value) {
    				$img[$key]['com_img_link']=$value;
    				$img[$key]['com_id']=$com;
    			}
    			$imgadd=array();
    			foreach ($img as $key => $value) {
    				$imgadd[]=M('ComImage')->add($value);
    			}
    			// header('Location:'.U('index'));
    		}
    		//写入分类
    		$cate=I('post.cate_name');
    		foreach ($cate as $key => $value) {
    			$catesave[$key]['cate_id']=$value;
    			$catesave[$key]['com_id']=$com;
    		}
    		foreach ($catesave as $key => $value) {
    			M('ComCate')->add($value);
    		}
            header('Location:'.U('index'));
    	}else{
    		
    		$ComModel = M('Commodity');
	        $comList = $ComModel->select();
	        $this->assign('comList',$comList);

	        $this->assign('action',U('addProduct'));
	        $this->assign('cancel',U('index'));

	        //加载视图类型静态文件
            $this->setStaticFiles('AdvancedForm');
	        $this->display();
    	}
	}

	public function editProduct(){
        if(IS_POST){
            $id = I('post.id',0,'intval');
            if($id<1){
                $this->error('错误的ID');
            }
            //写入商品表
            $data = array(
                'com_name' => I('post.com_name'),
                'com_number' => I('post.com_number'),
                'com_keywords' => I('post.com_keywords'),
                'com_discription' => I('post.com_discription'),
                'com_store' => I('post.com_store'),
                'com_price' => I('post.com_price'),
                'com_details' => I('post.com_details'),
                'com_addtime' => time(),
                'status' => I('post.status',0,'intval'),
                );
            $status = M('Commodity')->where("id=".$id)->save($data);//更新商品表
            //商品图片更新
            //写入图片表

            if ($id) {
                //先删除图片，再增加
                $imgdel=M('ComImage')->where('com_id='.$id)->delete();
                foreach (I('post.banner') as $key => $value) {
                    $img[$key]['com_img_name']=$value;
                }
                foreach (I('post.keywords') as $key => $value) {
                    $img[$key]['com_img_keyword']=$value;
                }
                foreach (I('post.link') as $key => $value) {
                    $img[$key]['com_img_link']=$value;
                    $img[$key]['com_id']=$id;
                }
                
                $imgadd=array();
                foreach ($img as $key => $value) {
                    $imgadd[]=M('ComImage')->add($value);
                }
            }

            // 更新商品——分类表
            //删除原有分类
            M('ComCate')->where('com_id='.$id)->delete();
            $cate=I('post.cate_name');
            foreach ($cate as $key => $value) {
                $catesave[$key]['cate_id']=$value;
                $catesave[$key]['com_id']=$id;
            }
            foreach ($catesave as $key => $value) {
                M('ComCate')->add($value);
            }
            header('Location:'.U('index'));
        }else{
            $id = I('get.id',0,'intval');
            if($id<1){
                $this->error('错误的ID');
            }

            $userInfo = M('Commodity')->where('id='.$id)->find();

            $this->comimg=M('ComImage')->where('com_id='.$id)->select();


            $result=M('ComCate')->field('cate_id')->where('com_id='.$id)->select();
            foreach ($result as $key => $value) {
                $cates[]=$value['cate_id'];
            }

            $this->getCategoryStr=getCategoryForm($cates);
            // $RoleModel = M('Role');
            // $roleList = $RoleModel->select();
            // $this->assign('roleList',$roleList);

            // $RoleUserModel = M('RoleUser');
            // $roleUserList = $RoleUserModel->where('user_id='.$id)->select();
            // foreach ($roleUserList as $userId) {
            //     $roleUserIds[] = $userId['role_id'];
            // }
            
            $this->assign('id',$id);
            $this->assign('userInfo',$userInfo);
            $this->assign('action',U('editProduct'));
            $this->assign('cancel',U('index'));

            //加载视图类型静态文件
            $this->setStaticFiles('AdvancedForm');
            $this->display();
        }
        
	}
	public function delProduct(){
        $id = I('get.id',0,'intval');
        if($id<1){
            $this->error('错误的ID');
        }
        M('Commodity')->where('id='.$id)->delete();
        M('ComImage')->where('com_id='.$id)->delete();
        M('ComCate')->where('com_id='.$id)->delete();
        header('Location:'.U('index'));
	}

}